package com.database.bean;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name="COMPANYPRODUCT")
@AttributeOverride( name="id", column = @Column(name="id") )
@XmlRootElement
public class CompanyProduct extends BaseBean{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@ManyToOne (fetch = FetchType.EAGER,cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
	@JoinColumn(name="productId")
	private Product product=null;   
	
	@ManyToOne
    @JoinColumn(name="decresId")
	private CompanyRoundDecisionAndResult companyRoundDecisionAndResult=null;
	
	@Min(0)
	@Column(name="sellingPrice")
	private Double sellingPrice=null;
	
	@Column(name="amountSold", columnDefinition="default '0'")
	private Integer amountSold=null;
	
	@OneToMany(fetch = FetchType.LAZY, cascade={CascadeType.ALL})
	@Fetch(value = FetchMode.SUBSELECT)
    @JoinColumn(name="companyproduct_id")
	private List<AgentTypeCompanyProduct>agentTypeCompanyProducts =new ArrayList<AgentTypeCompanyProduct>();
	
	@OneToMany(fetch = FetchType.LAZY, cascade={CascadeType.ALL})
	@Fetch(value = FetchMode.SUBSELECT)
    @JoinColumn(name="companyproduct_id")
	private List<Agent>agents=new ArrayList<Agent>();
	
	public CompanyProduct(){
		this.amountSold=0;
		this.sellingPrice=0.0;
	}

	public CompanyProduct getCopy(CompanyRoundDecisionAndResult decres){
		CompanyProduct cp = new CompanyProduct();
		cp.setAmountSold(0);
		cp.setSellingPrice(this.sellingPrice);
		cp.setProduct(this.product);
		cp.setCompanyRoundDecisionAndResult(decres);
		return cp;
	}
	
	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the sellingPrice
	 */
	public Double getSellingPrice() {
		return sellingPrice;
	}

	/**
	 * @param sellingPrice the sellingPrice to set
	 */
	public void setSellingPrice(Double sellingPrice) {
		this.sellingPrice = sellingPrice;
	}

	/**
	 * @return the amountSold
	 */
	public Integer getAmountSold() {
		if(this.amountSold==null){
			return 0;
		}
		return amountSold;
	}
	
	
	public void increaseAmountSold() {
		if(this.amountSold!=null)
			this.amountSold +=1;
		else
			this.amountSold = 1;
	}

	/**
	 * @param amountSold the amountSold to set
	 */
	public void setAmountSold(Integer amountSold) {
		this.amountSold = amountSold;
	}

	/**
	 * @return the companyRoundDecisionAndResult
	 */
	public CompanyRoundDecisionAndResult getCompanyRoundDecisionAndResult() {
		return companyRoundDecisionAndResult;
	}

	/**
	 * @param companyRoundDecisionAndResult the companyRoundDecisionAndResult to set
	 */
	public void setCompanyRoundDecisionAndResult(
			CompanyRoundDecisionAndResult companyRoundDecisionAndResult) {
		this.companyRoundDecisionAndResult = companyRoundDecisionAndResult;
	}

	@Override
	public String toString() {
		return "CompanyProduct [product=" + product
				+ ", companyRoundDecisionAndResult="
				+ companyRoundDecisionAndResult + ", sellingPrice="
				+ sellingPrice + ", amountSold=" + amountSold
				+ ", agentTypeCompanyProducts=" + agentTypeCompanyProducts
				+ ", agents=" + agents + "]";
	}

	/**
	 * @return the agentTypeCompanyProducts
	 */
	public List<AgentTypeCompanyProduct> getAgentTypeCompanyProducts() {
		return agentTypeCompanyProducts;
	}

	/**
	 * @param agentTypeCompanyProducts the agentTypeCompanyProducts to set
	 */
	public void setAgentTypeCompanyProducts(
			List<AgentTypeCompanyProduct> agentTypeCompanyProducts) {
		this.agentTypeCompanyProducts = agentTypeCompanyProducts;
	}

	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}
	
	
}
